﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BEGDC.PreContract.BLL;

namespace PreContractWeb.Administration
{
    public partial class RateManagement1 : System.Web.UI.Page
    {
        private BEGDC.PreContract.BLL.Authentication.PCIdentity user;

        protected void Page_Load(object sender, EventArgs e)
        {

      
            user = new BEGDC.PreContract.BLL.Authentication.PCIdentity(HttpContext.Current.User.Identity);

            if (!ReportHelper.IsAdmin(user.EmployeeId))
            {
                this.divError.InnerHtml = AlertMessage.NoAdminPermission;
                this.divConnect.Visible = false;
                this.divError.Visible = true;
            }

            else if (!Page.IsPostBack)
            {
                this.LoadData();
            }
        }

        protected void LoadData()
        {
                try
                {
                    BEGDC.PreContract.BLL.IExchangeRate rate = (BEGDC.PreContract.BLL.IExchangeRate)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_ExchangeRate_Service];

                    DataTable dt = rate.GetExchangeRate_DataTable(String.Empty);
                    if (dt.Rows.Count == 0)
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('"+BaseFunction.ChangeAlertMessage(AlertMessage.NoDataExist)+"')</script>");
                        return;
                    }
                    
                    BEGDC.PreContract.DAL.ExchangeRate exchangeRate = rate.GetExchangeRate(Convert.ToInt32(dt.Rows[0]["ExchangeRateID"].ToString()));
                    this.RateTextBox.Text = exchangeRate.Rate.ToString();
                    if (exchangeRate.UpdateDate != Nullables.NullableDateTime.Default)
                    {
                        DateTime date =System.Convert.ToDateTime(exchangeRate.UpdateDate.Value);
                        this.UpdateDateTimeLabel.Text = date.ToString("yyyy-MM-dd") + " " + date.ToShortTimeString();
                    }
                }
                catch (Exception ex)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
                }
        }

        protected void SureButton_Click(object sender, EventArgs e)
        {
            try
            {
             
                BEGDC.PreContract.BLL.IExchangeRate rate = (BEGDC.PreContract.BLL.IExchangeRate)BEGDC.Utility.Castle.AppContainer.Instance[BEGDC.PreContract.BLL.Services.CONST_ExchangeRate_Service];
                BEGDC.PreContract.DAL.ExchangeRate exchangeRate = rate.GetExchangeRate(1);

                double exchangeRateNow = Double.Parse(this.RateTextBox.Text);


                if (rate.UpdateExchangeRate(1, new Nullables.NullableDecimal(Convert.ToDecimal(exchangeRateNow)), new Nullables.NullableDateTime(System.DateTime.Now)))
                {
                    BaseFunction.CreateLog(BEGDC.PreContract.BLL.OperationLog.OperationType.EditExchangeRate, string.Format("ExchangeRate:{0}", exchangeRateNow.ToString()), User.Identity.Name);

                    this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + BaseFunction.ChangeAlertMessage(AlertMessage.OperationSuccess) + "')</script>");
                }
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('" + ex.Message.Replace("'", "\\'") + "')</script>");
            }
            
        }
    }
}
